package com.powernode.controller;


import com.powernode.common.Result;
import com.powernode.domain.StatBean;
import com.powernode.dto.StatDto;
import com.powernode.service.impl.StatService;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/stat")
public class StatController {

    @Autowired
    private StatService statService;


    /**
     * 拜访量统计
     * //
     */
    @RequestMapping("/statVisitLogCount.do")
    @ResponseBody
    public Result statVisitLogCount(StatDto statDto) {
        System.out.println(statDto + "------------" + "我是测试");
        List<StatBean> statBeanList = this.statService.queryVisitLogCount(statDto);
        try {
//            把values与names放入到map中去
            ArrayList<String> names = new ArrayList<>();
            ArrayList<Integer> values = new ArrayList<>();
            for (StatBean statBean : statBeanList) {
                names.add(statBean.getName());
                values.add(statBean.getValue());
            }
            HashMap<String, Object> map = new HashMap<>();
            map.put("name", names);
            map.put("value", values);
            return new Result(200, "查询成功", 0L, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(-1, "查询失败");
        }
    }
}
